package com.utils;

import java.util.UUID;

/**
 * 纯数字UUID生成工具类
 */
public class NumericUUIDUtil {

    /**
     * 生成纯数字的UUID
     * @return 纯数字的UUID字符串
     */
    public static String generateNumericUUID() {
        // 生成标准UUID
        UUID uuid = UUID.randomUUID();
        // 转换为纯数字形式
        return convertToNumeric(uuid);
    }

    /**
     * 将UUID转换为纯数字形式
     * @param uuid 标准UUID
     * @return 纯数字形式的UUID字符串
     */
    private static String convertToNumeric(UUID uuid) {
        // 获取UUID的高64位和低64位
        long mostSigBits = uuid.getMostSignificantBits();
        long leastSigBits = uuid.getLeastSignificantBits();
        
        // 确保数字为正数
        mostSigBits = mostSigBits < 0 ? -mostSigBits : mostSigBits;
        leastSigBits = leastSigBits < 0 ? -leastSigBits : leastSigBits;
        
        // 组合成纯数字字符串
        return String.format("%016d%016d", mostSigBits, leastSigBits);
    }

    /**
     * 生成较短长度的纯数字UUID
     * @param length 期望的长度（最大32）
     * @return 指定长度的纯数字UUID
     */
    public static String generateShortNumericUUID(int length) {
        if (length <= 0 || length > 32) {
            throw new IllegalArgumentException("Length must be between 1 and 32");
        }
        
        String fullNumericUUID = generateNumericUUID();
        return fullNumericUUID.substring(0, length);
    }

    // 测试方法
    public static void main(String[] args) {
        System.out.println("完整数字UUID: " + generateNumericUUID());
        System.out.println("16位数字UUID: " + generateShortNumericUUID(16));
        System.out.println("8位数字UUID: " + generateShortNumericUUID(8));
    }
}